//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.SecurityControls
{
    using System.Windows;
    using System.Windows.Input;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using System.Collections.Generic;
    using Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.ComponentModel;
    using System.Windows.Data;

    /// <summary>
    /// Interaction logic for PermissionSettingsCtrl.xaml
    /// </summary>
    public partial class PermissionSettingsCtrl : Window
    {
        /// <summary>
        /// Users and groups.
        /// </summary>
        private IEnumerable<ISecurityPrincipal> usersAndGroupsSet;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PermissionSettingsCtrl"/> is canceled.
        /// </summary>
        /// <value><c>true</c> if canceled; otherwise, <c>false</c>.</value>
        public bool Canceled { get; set; }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public PermissionSettingsModel Model
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PermissionSettingsCtrl"/> class.
        /// </summary>
        /// <param name="securableObject">The securable object.</param>
        public PermissionSettingsCtrl(PermissionSettingsModel permissionSettingsModel, IEnumerable<ISecurityPrincipal> usersAndGroupsSet)
        {
            InitializeComponent();
            this.Model = permissionSettingsModel;
            this.usersAndGroupsSet = usersAndGroupsSet;
            this.DataContext = this.Model;
        }
        
        /// <summary>
        /// Called when header mouse down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnHeaderMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// Called when ok click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnOkClick(object sender, RoutedEventArgs e)
        {
            this.Canceled = false;
            this.Close();
        }

        /// <summary>
        /// Called when cancel click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            this.Canceled = true;
            this.Close();
        }

        /// <summary>
        /// Called when add user click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnAddUserClick(object sender, RoutedEventArgs e)
        {
            UsersAndGroupsPicker usersAndGroupsPicker = new UsersAndGroupsPicker(usersAndGroupsSet);
            usersAndGroupsPicker.Owner = this;
            usersAndGroupsPicker.ShowDialog();

            if (!usersAndGroupsPicker.Canceled && usersAndGroupsPicker.usersAndGroupsListView.SelectedItem != null)
            {
                SecurityPrincipalModel securityPrincipalModelToAdd = usersAndGroupsPicker.usersAndGroupsListView.SelectedItem as SecurityPrincipalModel;
                if (securityPrincipalModelToAdd != null)
                {
                    try
                    {
                        SecurityPermissionModel newModel = this.Model.CreateNewSecurityPrincipal(securityPrincipalModelToAdd.SecurityPrincipal);
                        if (newModel != null && this.lstBxSecurityPrincipals.Items.Contains(newModel))
                        {
                            this.lstBxSecurityPrincipals.SelectedIndex = this.lstBxSecurityPrincipals.Items.IndexOf(newModel);
                        }
                    }
                    catch (TridentCustomException ex)
                    {
                        TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
                    }
                }
            }
        }

        /// <summary>
        /// Called when remove user click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnRemoveUserClick(object sender, RoutedEventArgs e)
        {
            if (this.lstBxSecurityPrincipals != null && this.lstBxSecurityPrincipals.SelectedItem != null)
            {
                SecurityPermissionModel securityPermissionModel = this.lstBxSecurityPrincipals.SelectedItem as SecurityPermissionModel;
                this.Model.RemoveSecurityPrincipal(securityPermissionModel);
            }
        }
    }
}
